package com.jt.controller;

import com.jt.mapper.CartMapper;
import com.jt.pojo.Cart;
import com.jt.pojo.IntClass;
import com.jt.pojo.Shopping;
import com.jt.pojo.User;
import com.jt.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private CartMapper cartMapper;

    //添加购物车
    @PostMapping("/addCart")
    public void addCart(@RequestBody Cart cart){
        this.cartService.addCart(cart);
    }
    //查询购物车
    @RequestMapping("/findCart")
    public List<Shopping> findCart(@RequestBody User user){
        List<Shopping> cartList =cartService.findCart(user.getId());
        if(cartList == null){
            return null;
        }
        return cartList;
    }
    //减少购物车中的商品
    @RequestMapping("/decreaseCart")
    public void decreaseCart(@RequestBody Shopping shopping){
        if(shopping.getNummer()==1){
            cartMapper.deleteById(shopping.getId());
        }
        cartService.decreaseCart(shopping);
    }
    //增加购物车中的商品
    @RequestMapping("/addCart1")
    public void addCart(@RequestBody Shopping shopping){
        cartService.addCart(shopping);
    }
    //删除购物车
    @RequestMapping("/deleteCart")
    public void deleteCart(@RequestBody Shopping shopping){
        cartService.deleteCart(shopping.getId());
    }
    @RequestMapping("/deleteAllCart")
    public void deleteAllCart(@RequestBody User user){
        cartService.deleteAllCart(user.getId());
    }
}
